package com.mall.service.authority.controller;

import com.mall.commons.common.CommonPage;
import com.mall.commons.common.CommonResult;
import com.mall.model.domain.UmsResource;
import com.mall.service.authority.service.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author Pan Rundong
 * @Date 2023-03-11 21:59
 * @Desc 后台资源Controller
 */
@RestController
@RequestMapping("resource")
@Slf4j
public class ResourceController {
    @Autowired
    private ResourceService resourceService;

    /**
     * 添加后台资源
     *
     * @param resource
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("create")
    public CommonResult<String> create(@RequestBody UmsResource resource) {
        log.info("resourceCategory = {}", resource.toString());
        Boolean flag = resourceService.checkAndSave(resource);
        if (flag) {
            return CommonResult.success(null, "后台资源添加成功");
        } else {
            return CommonResult.error("后台资源名重复");
        }
    }

    /**
     * 根据Id删除后台资源
     *
     * @param id
     * @return com.mall.commons.common.CommonResult<java.lang.String>
     * @Author Pan Rundong
     */
    @PostMapping("delete/{id}")
    public CommonResult<String> delete(@PathVariable Long id) {
        log.info("id为{}" + id);
        resourceService.removeById(id);
        return CommonResult.success(null, "删除后台资源成功");
    }

    /**
     * 分页查询后台资源
     *
     * @param pageNum
     * @param pageSize
     * @param categoryId
     * @param nameKeyword
     * @param urlKeyword
     * @return com.mall.commons.common.CommonResult<com.mall.commons.common.CommonPage < com.mall.model.domain.UmsResource>>
     * @Author Pan Rundong
     */
    @GetMapping("list")
    public CommonResult<CommonPage<UmsResource>> list(@RequestParam(defaultValue = "1") Integer pageNum,
                                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                                      Long categoryId, String nameKeyword, String urlKeyword)
    {
        log.info("pageNum = {}, pageSize = {}, categoryId = {}, name = {}, urlKeyword = {}",
                pageNum, pageSize, categoryId, nameKeyword, urlKeyword);
        CommonPage<UmsResource> page = resourceService.findAll(pageNum, pageSize,
                categoryId, nameKeyword, urlKeyword);
        return CommonResult.success(page, "查询成功");
    }

    /**
     * 查询所有后台分类
     *
     * @param
     * @return com.mall.commons.common.CommonPage<java.util.List < com.mall.model.domain.UmsResource>>
     * @Author Pan Rundong
     */
    @GetMapping("listAll")
    public CommonResult<List<UmsResource>> listAll() {
        log.info("查询所有后台分类");
        List<UmsResource> list = resourceService.list();
        return CommonResult.success(list, "查询成功");
    }

    /**
     * 修改后台资源分类
     *
     * @param id
     * @return com.mall.commons.common.CommonResult<com.mall.model.domain.UmsResource>
     * @Author Pan Rundong
     */
    @PostMapping("update/{id}")
    public CommonResult<UmsResource> update(@PathVariable Long id, @RequestBody UmsResource resource) {
        log.info("resourceCategory = {}", resource.toString());
        Boolean flag = resourceService.checkAndUpdate(id, resource);
        if (flag) {
            return CommonResult.success(null, "后台资源修改成功");
        } else {
            return CommonResult.error("后台资源修改失败");
        }
    }

    /**
     * 根据Id查询后台资源
     *
     * @param id
     * @return com.mall.commons.common.CommonResult<com.mall.model.domain.UmsResource>
     * @Author Pan Rundong
     */
    @GetMapping("{id}")
    public CommonResult<UmsResource> findById(@PathVariable Long id) {
        log.info("id = {}", id);
        UmsResource umsResource = resourceService.getById(id);
        if (umsResource != null) {
            return CommonResult.success(umsResource, "查询成功");
        } else {
            return CommonResult.error("未查到后台资源信息");
        }
    }
}
